<?php

/**
 * This is the model class for table "tbsanpham".
 *
 * The followings are the available columns in table 'tbsanpham':
 * @property integer $id
 * @property string $tensanpham
 * @property string $masanpham
 * @property integer $iddonvitinh
 * @property string $mota
 *
 * The followings are the available model relations:
 * @property Tbchitietthuehang[] $tbchitietthuehangs
 * @property TbkhachhangNosp[] $tbkhachhangNosps
 * @property Tbdonvitinh $iddonvitinh0
 * @property Tbtonkho[] $tbtonkhos
 */
class Tbsanpham extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Tbsanpham the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
	
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'tbsanpham';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		$message ="<span class='ui-icon ui-icon-alert'></span><span class='message'>". Yii::t('yii','Vui lòng nhập {attribute}.')."</span>";
		return array(
			array('tensanpham,masanpham', 'required','message'=>$message),
			array('iddonvitinh', 'numerical', 'integerOnly'=>true),
			array('tensanpham', 'length', 'max'=>100),
			array('masanpham', 'length', 'max'=>20),
			array('mota', 'length', 'max'=>200),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, tensanpham, masanpham, iddonvitinh, mota', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'tbchitietthuehangs' => array(self::HAS_MANY, 'Tbchitietthuehang', 'idsanpham'),
			'tbkhachhangNosps' => array(self::HAS_MANY, 'TbkhachhangNosp', 'idsanpham'),
			'tbdonvitinh' => array(self::BELONGS_TO, 'Tbdonvitinh', 'iddonvitinh'),
			'tbtonkhos' => array(self::HAS_MANY, 'Tbtonkho', 'idsanpham'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'tensanpham' => 'Tên sản phẩm',
			'masanpham' => 'Mã sản phẩm',
			'iddonvitinh' => 'Đơn vị tính',
			'mota' => 'Mô tả',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('tensanpham',$this->tensanpham,true);
		$criteria->compare('masanpham',$this->masanpham,true);
		$criteria->compare('iddonvitinh',$this->iddonvitinh);
		$criteria->compare('mota',$this->mota,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
}